package org.popupcommunication.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.popupcommunication.model.Agenda;
import org.popupcommunication.model.User;
import org.popupcommunication.service.UserManager;
import org.popupcommunication.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
	@Autowired UserManager userService;	

	@RequestMapping(value={"index","/"})
	public String showIndex (HttpServletRequest request,Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);
	    String path = null;
	    if (toto.getSrcAvatar() != null && toto.getSrcAvatar().isEmpty() == false){
	    	path = request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/"+toto.getSrcAvatar();
	    }
	    else{
	    	path = request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/no_avatar.jpg";	    	
	    }
	    model.addAttribute("utilisateur", toto);
	    model.addAttribute("avatar", path);
	    List<Agenda> currentsEvents = userService.getCurrentsEventsUsers(toto);
	    model.addAttribute("currentAgendaGroupes", currentsEvents);
	    List<Agenda> currentsEventsPerso = userService.getCurrentsEventsPersoUsers(toto);
	    model.addAttribute("currentAgendaPerso", currentsEventsPerso);
//	logger.info("Return Index View");
	return "index";
	}
}
